#include <iostream>
#include "igslib_iutil.h"
#include "igslib_ipacket.h"

namespace igslib
{
    namespace packet_sample
    {
        /*!
         * This sample demostrates how to use `IPacket`.
         * At runtime, we prepare a simple buffer to import to one `IPacket` only,
         * then we get some information from it.
		 * note: this additions to u8_checksum of self
         */
        static void PacketImportRawData()
        {
            const char raw[] = 
            {
                1, // header
                2, // length
                3,5 // data
            };  
            IPacketManager* mgr = GetPacketManagerInstance();
            IPacket* packet = mgr->NewPacket();
            int import_result = packet->ImportRawData(raw);
            util::Log("%s/import_result = %d, header=0x%02x,data_length=0x%02x,data=",
                    __func__,
                    import_result,
                    packet->GetHeaderID(),
                    packet->GetData().size());

            auto buf = packet->GetData();
            for ( int i = 0 ; i < buf.size()-1 ; ++i )
            {
                util::Log("0x%02x,",buf[i]);
            }
            util::Log("\n");
            util::Log("unique_id=0x%02x\n",packet->GetSelfUniqueID());
            util::Log("checksum=0x%02x\n",packet->GetSelfu8Checksum());
            exit(EXIT_SUCCESS);
        }
        /*!
         * This sample demostrates another way to set `IPacket` data.
         * At runtime, we JUST set header and data to `IPacket`, we 
         * get some information from it, you'll see the length is also
         * be set automatically.
         */
        static void PacketSetData()
        {
            IPacketManager* mgr = GetPacketManagerInstance();
            IPacket* packet = mgr->NewPacket();
            // 1st setup.
            {
                std::string str = "abcdefg";
                std::vector<char> data(str.begin(),str.end());

                packet->SetHeaderID(0xab);
                packet->SetData(data);
                util::Log("%s#1/header=0x%02x,length=0x%02x,data:",
                        __func__,
                        packet->GetHeaderID(),
                        packet->GetData().size());

                auto buf = packet->GetData();
                for ( int i = 0 ; i < buf.size() ; ++i )
                {
                    util::Log("0x%02x,",buf[i]);
                }
                util::Log("\n");
            }
            // 2nd setup.
            {
                packet->SetHeaderID(0xc5);
                std::string str = "xyz";
                std::vector<char> data(str.begin(),str.end());
                packet->SetData(data);
                util::Log("%s#2/header=0x%02x,length=0x%02x,data:",
                        __func__,
                        packet->GetHeaderID(),
                        packet->GetData().size());

                auto buf = packet->GetData();
                for ( int i = 0 ; i < buf.size() ; ++i )
                {
                    util::Log("0x%02x,",buf[i]);
                }
                util::Log("\n");
            }
            exit(EXIT_SUCCESS);
        }
        /*!
         * This sample demostrates how to create multiple `IPacket` from LONG raw buffer.
         * At runtime, we prepare a long buffer for manager to create packets.
         * Then we will get some information from them.
         */
        static void NewPacketsWithRawData()
        {
            const char raw[] = 
            {
                0x01,                                  // header
                0x02,                                  // length
                0x03,0x04,                             // data

                0xab,                                  // header 
                5,                                     // length 
                0x0a,0x0b,0x0c,0x0d,0x0e,              // data   

                0xc3,                                  // header 
                3,                                     // length 
                0x11,0xaa,0xff,                        // data   
            };
            IPacketManager* mgr = GetPacketManagerInstance();
            std::vector<IPacket*> packets = mgr->NewPacketsWithRawData(raw,sizeof(raw));
            for(auto& each:packets)
            {
                util::Log("%s/header=0x%02x,length=0x%02x,data:",
                        __func__,
                        each->GetHeaderID(),
                        each->GetData().size());

                auto buf = each->GetData();
                for ( int i = 0 ; i < buf.size() ; ++i )
                {
                    util::Log("0x%02x,",buf[i]);
                }
                util::Log("\n");
            }
            exit(EXIT_SUCCESS);
        }
    };
};

